var ipconfig = "http://192.168.3.133:8080"//服务器ip地址
Page({
  data: {
    showTopTips: true,
    
    

    radioItems: [
      { name: 'cell standard', value: '0' },
      { name: 'cell standard', value: '1', checked: true }
    ],
    checkboxItems: [
      { name: 'standard is dealt for u.', value: '0', checked: true },
      { name: 'standard is dealicient for u.', value: '1' }
    ],
    hostIndex:0,
    host_housenumberIndex: 0,
    accounts: ["桐梓林一号"],
    accountIndex: 0,
    isAgree: false
  },
  onLoad:function(){
    
  },
  onShow:function(){
    var that = this;
    wx.request({
      url: ipconfig+"/Wxregister",
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function (res) {
        console.log(res.data)
        var host_housenumber = [];
        var host_building = [];
        var host_unit = [];
        var host = [];
        var rst=[];
        var test=[];
        for (var i = 0; i < res.data.length; i++) {
          host_housenumber.push(res.data[i].host_housenumber)
          host_building.push(res.data[i].host_building)
          host_unit.push(res.data[i].host_unit)
          host.push(res.data[i].host_building + "栋" + res.data[i].host_unit + "单元")
          rst.push(res.data[i].host_building)
          test.push(res.data[i].host_unit)
        }
        that.setData({
          host_housenumber: host_housenumber,
          host: host,
          rst:rst,
          test:test,

        })
      }
    })

  },
  showTopTips: function () {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems
    });
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);

    var checkboxItems = this.data.checkboxItems, values = e.detail.value;
    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      checkboxItems[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkboxItems[i].value == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    this.setData({
      checkboxItems: checkboxItems
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  //this.data.countries[this.data.countryIndex]
  formSubmit: function (e) {
    var that=this;
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var host_name = e.detail.value.name;
    var account=e.detail.value.username;
    var host_phone=e.detail.value.phone;
    var host = that.data.host[that.data.hostIndex];
    var host_housenumber = that.data.host_housenumber[this.data.host_housenumberIndex];
    var rst=that.data.rst[that.data.hostIndex];
    var test=that.data.test[that.data.hostIndex];
    var srt = /^1[345789]\d{9}$/;
    var username = /^[\u4E00-\u9FA5]+$/;
    var username1 = /^[\u4E00-\u9FA5]+$/;
    if(srt.test(host_phone)&&username.test(host_name)&&username1.test(account)){
      wx.request({
        url: ipconfig+'/Wxsave',
        method: "post",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': wx.getStorageSync("session")
        },
        data: {
          host_name: host_name,
          host_phone: host_phone,
          host: host,
          host_housenumber: host_housenumber,
          rst:rst,
          test:test,
          accounts:account,
        },
        success: function (res) {
          if(res.data==true){
          wx.redirectTo({
            url: '../index/home-page',
          })
          }else{
            wx.showToast({
              title: '输入信息有误',
              image: '../imgages/failure.png',
            })
          }
        }
      })
    }else{
      if (!(srt.test(host_phone))&&username.test(host_name)){
        wx.showToast({
          title: '手机号错误',
          image: '../imgages/failure.png',
        })
      } else if ((srt.test(host_phone)) && !(username.test(host_name))){
        wx.showToast({
          title: '姓名有误',
          image: '../imgages/failure.png',
          
        })
      }else{
        wx.showToast({
          title: '手机和姓名有误',
          image:'../imgages/failure.png',
          
        })
      }
    }
  },
  //bindCountryChanges
  bindCountryCodeChange: function (e) {
    console.log('picker country code 发生选择改变，携带值为', e.detail.value);

    this.setData({
      countryCodeIndex: e.detail.value
    })
  },
  bindCountryChange: function (e) {
    console.log('picker host_housenumber发生选择改变，携带值为', e.detail.value);

    this.setData({
      host_housenumberIndex: e.detail.value
    })
  },
  bindCountryChanges: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);

    this.setData({
      hostIndex: e.detail.value
    })
  },

  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  showTopTips:function(e){
    console.log(e)
    // wx.redirectTo({
    //   url: '../index/home-page'
    // })
   
  },

});